<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Attractions\Commerce\Repository\Commerce_commodity;

use Plugin\Attractions\Commerce\Model\Commerce_commodity\CommerceCommodity;
use Hyperf\Database\Model\Builder;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;

/**
 * 商品 Repository类
 */
class CommerceCommodityRepository extends IRepository
{
   public function __construct(
        protected readonly CommerceCommodity $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 主键ID
        if (isset($params['id']) && !empty($params['id'])) {
            $query->whereIn('id', Arr::wrap($params['id']));
        }

        // 商户ID
        if (isset($params['merchant_id']) && !empty($params['merchant_id'])) {
            $query->where('merchant_id', '=', $params['merchant_id']);
        }

        // 商品名称
        if (isset($params['commodity_name']) && !empty($params['commodity_name'])) {
            $query->where('commodity_name', 'like', '%'.$params['commodity_name'].'%');
        }

        // 商品分类
        if (isset($params['commodity_category']) && !empty($params['commodity_category'])) {
            $query->where('commodity_category', '=', $params['commodity_category']);
        }

        // 商品品牌
        if (isset($params['commodity_brand']) && !empty($params['commodity_brand'])) {
            $query->where('commodity_brand', 'like', '%'.$params['commodity_brand'].'%');
        }

        // 商品型号
        if (isset($params['commodity_model']) && !empty($params['commodity_model'])) {
            $query->where('commodity_model', 'like', '%'.$params['commodity_model'].'%');
        }

        // 商品规格
        if (isset($params['commodity_spec']) && !empty($params['commodity_spec'])) {
            $query->where('commodity_spec', '=', $params['commodity_spec']);
        }

        // 计量单位
        if (isset($params['commodity_unit']) && !empty($params['commodity_unit'])) {
            $query->where('commodity_unit', 'like', '%'.$params['commodity_unit'].'%');
        }

        // 商品描述
        if (isset($params['commodity_description']) && !empty($params['commodity_description'])) {
            $query->where('commodity_description', '=', $params['commodity_description']);
        }

        // 销售价
        if (isset($params['sale_price']) && !empty($params['sale_price'])) {
            $query->where('sale_price', '=', $params['sale_price']);
        }

        // 折扣
        if (isset($params['discount']) && !empty($params['discount'])) {
            $query->where('discount', '=', $params['discount']);
        }

        // 库存数量
        if (isset($params['stock_quantity']) && !empty($params['stock_quantity'])) {
            $query->where('stock_quantity', '=', $params['stock_quantity']);
        }

        // 商品图片（JSON格式）
        if (isset($params['commodity_images']) && !empty($params['commodity_images'])) {
            $query->where('commodity_images', '=', $params['commodity_images']);
        }

        // 商品状态：1-上架，2-下架
        if (isset($params['status']) && !empty($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        // 排序
        if (isset($params['sort_order']) && !empty($params['sort_order'])) {
            $query->where('sort_order', '=', $params['sort_order']);
        }

        // 备注
        if (isset($params['remark']) && !empty($params['remark'])) {
            $query->where('remark', '=', $params['remark']);
        }

        // 
        if (isset($params['created_at']) && !empty($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 
        if (isset($params['updated_at']) && !empty($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        return $query;
    }
}